
function specialCheckItemListCtrl($scope, $http, ngDialog) {
  'ngInject';
  this.specialCheckData = {};
  let id = $state.params.id;
  this.id = id;

  this.incompletedTask = [];

  // 分页
  this.paginationData = {
    pageNum: 1,
    pageSize: 10,
    taskId: this.id,
    total: '',
  }

  let isInspectionCompleted = () => {
    let checked = true;
    let innerList = this.specialCheckData.securityCheckClassifyMap.inner;
    let outerList = this.specialCheckData.securityCheckClassifyMap.outer;

    for(let i = 0; i < innerList.length; i++) {
      if (!innerList[i].check) {
        checked = false;
        this.incompletedTask.push(innerList[i]);
      }
    }

    for(let i = 0; i < outerList.length; i++) {
      if (!outerList[i].check) {
        checked = false;
        this.incompletedTask.push(outerList[i]);
      }
    }
    return checked;
  }

  this.isBtnDisabled = () => {
    if (!this.checked && this.specialCheckData.status == 1) {
      return true;
    } else {
      return false;
    }
  }

  // 获取专项检查信息
  this.specialInspection = () => {
    $http.get(`${SERVER_CONTEXT}/api/securityActivities/specialCheck/querySpecialCheckById/${id}`)
      .then((resp) => {
        this.specialCheckData = resp.data;
        this.checked = isInspectionCompleted();
      });
  };

  // 获取专项检查列表
  this.getSpecial = () => {
    $http.post(`${SERVER_CONTEXT}/api/securityActivities/checkProblem/queryCheckProblemListByCondition`,
      this.paginationData
        )
      .then((resp) =>{
        this.specialCheckList = resp.data.data;
        this.paginationData.total = resp.data.total;        
    });
  }

  // 页码变化时调用方法
  $scope.pageChange = () => {
    $http.post(`${SERVER_CONTEXT}/api/securityActivities/checkProblem/queryCheckProblemListByCondition`,
      this.paginationData
        )
      .then((resp) => {
        this.specialCheckList = resp.data.data;
        this.paginationData.total = resp.data.total;
    });
  }; 

  this.getCheckStatus = () => {
    let status = this.specialCheckData.status;
    switch(status) {
      case 0:
        return '未开始';
        break;
      case 1:
        return '进行中';
        break;
      case 2:
        return '待整改';
        break;
      case 3:
        return '已完成';
        break;
    }
  }

  // 获得按钮文字
  this.getBtnStatus = () => {
    let status = this.specialCheckData.status;
    switch(status) {
      case 0:
        return '开始检查';
        break;
      case 1:
        return '完成检查';
        break;
      case 2:
        return '完成检查';
        break;
      default:
        return '已完成';
    }
  }

  // 考核项目内业、外业的弹框
  this.openItemDetail = function(item) {
    let _this = this;
    ngDialog.open({
      template: 'specialItemDetailDialog',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme',
      scope: $scope,
      controllerAs: 'vm',
      controller: function specialDetailCtrl($http, $scope) {
        'ngInject'; 
        console.log(item);
        this.itemDetail1 = item;   
      }
    });
  }

  // 更改任务状态
  this.changeTaskStatus = () => {
    let status = this.specialCheckData.status;

    // 开始检查
    if (status == 0) {
      $http.put(`${SERVER_CONTEXT}/api/securityActivities/specialCheck/startCheckById/${this.id}`)
        .then((resp) => {
          if(resp.data.success){
            layer.msg(resp.data.message);
            $state.reload(true);
          }
        });
    }

    // 进行中
    if (status == 1) {
      if (this.incompletedTask.length) {
        layer.alert('检查还在进行中,还有未完成项目');
      } else {
        $http.put(`${SERVER_CONTEXT}/api/securityActivities/specialCheck/completeCheckById/${this.id}`)
          .then((resp) => {
            if(resp.data.success){
              layer.msg(resp.data.message);
              $state.reload(true);
            }
          });
      }
      
    }
  }

  // 初始化页面
  this.initPage = () => {
    // 获取专项检查信息
    this.specialInspection();

    // 获取专项检查列表
    this.getSpecial();
  }

  this.initPage();
}

export default specialCheckItemListCtrl;